﻿using System;
using CookComputing.XmlRpc;

namespace CRMWeb.Utilities
{
    // Replace the hostname in this example with your system Virtual Host
    [XmlRpcUrl("http://www.vision6.com.au/api/xmlrpcserver.php?version=1.2")]
    public class XmlRpcWrapper : XmlRpcClientProtocol
    {
        #region Login/Authentication Methods

        [XmlRpcMethod("login")]
        public Object login(string username, string password)
        {
            string response;

            try
            {
                response = (string)Invoke("login", new Object[] { username, password });
            }
            catch (Exception ex)
            {
                throw new Exception("XmlRpc Login failed" + ex.Message);
            }

            this.Url = response;
            return true;
        }

        [XmlRpcMethod("isLoggedIn")]
        public Object isLoggedIn()
        {
            XmlRpcStruct response;
            try
            {
                response = (XmlRpcStruct)Invoke("isLoggedIn", new Object[] { });
            }
            catch
            {
                return false;
            }

            if (response["0"].GetType() == typeof(bool))
            {
                bool success = (bool)response["0"];
                return success;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region Folders methods

        [XmlRpcMethod("searchFolders")]
        public Object searchFolders(string type, Object[] criteria)
        {
            return Invoke("searchFolders", new Object[] { type, criteria });
        }

        #endregion

        #region Database Methods

        [XmlRpcMethod("getDatabaseById")] //NEW
        public Object getDatabaseById(int database_id)
        {
            return Invoke("getDatabaseById", new Object[] { database_id });
        }
        [XmlRpcMethod("getDatabaseByName")] //NEW
        public Object getDatabaseByName(string database_name)
        {
            return Invoke("getDatabaseByName", new Object[] { database_name });
        }

        [XmlRpcMethod("addDatabase")]
        public Object addDatabase(XmlRpcStruct database)
        {
            return Invoke("addDatabase", new Object[] { database });
        }

        [XmlRpcMethod("addField")]
        public Object addField(int database_id, XmlRpcStruct field)
        {
            return Invoke("addField", new Object[] { database_id, field });
        }

        #endregion

        #region Contacts Methods

        [XmlRpcMethod("addContacts")]
        public Object addContacts(int database_id, XmlRpcStruct contacts)
        {
            return Invoke("addContacts", new Object[] { database_id, contacts });
        }

        [XmlRpcMethod("unsubscribeContact")]
        public Object unsubscribeContact(string email, int database_id)
        {
            return Invoke("unsubscribeContact", new Object[] { email, database_id });
        }



        [XmlRpcMethod("searchContacts")]
        public Object searchContacts(int database_id, Object[] criteria)
        {
            return Invoke("searchContacts", new Object[] { database_id, criteria });
        }

        #endregion

        #region Emails Methods

        [XmlRpcMethod("searchEmails")]
        public Object searchEmails(XmlRpcStruct criteria, int limit, int offset)
        {
            return Invoke("searchEmails", new Object[] { criteria, limit, offset });
        }
        [XmlRpcMethod("getEmailById")] //NEW
        public Object getEmailById(int email_id)
        {
            return Invoke("getEmailById", new Object[] { email_id });
        }
        [XmlRpcMethod("addEmail")] //NEW
        public Object addEmail(XmlRpcStruct email)
        {
            return Invoke("addEmail", new Object[] { email });
        }

        #endregion

        #region Campaigns Methods

        [XmlRpcMethod("getCampaignStatus")]
        public Object getCampaignStatus(int campaign_id)
        {
            return Invoke("getCampaignStatus", new Object[] { campaign_id });
        }

        [XmlRpcMethod("getCampaignStatistics")]
        public Object getCampaignStatistics(int campaign_id)
        {
            return Invoke("getCampaignStatistics", new Object[] { campaign_id });
        }

        [XmlRpcMethod("getCampaignLinkStatistics")]
        public Object getCampaignLinkStatistics(int campaign_id)
        {
            return Invoke("getCampaignLinkStatistics", new Object[] { campaign_id });
        }

        [XmlRpcMethod("getCampaignContactLinks")]
        public Object getCampaignContactLinks(int campaign_id, int send_id)
        {
            return Invoke("getCampaignContactLinks", new Object[] { campaign_id, send_id });
        }

        [XmlRpcMethod("getCampaignOpened")]
        public Object getCampaignOpened(int campaign_id)
        {
            return Invoke("getCampaignOpened", new Object[] { campaign_id });
        }

        [XmlRpcMethod("addCampaign")]
        public Object addCampaign(int email_id, XmlRpcStruct campaigns)
        {
            return Invoke("addCampaign", new Object[] { email_id, campaigns });
        }

        [XmlRpcMethod("getCampaignIdByQueueId")]
        public Object getCampaignIdByQueueId(int queue_id)
        {
            return Invoke("getCampaignIdByQueueId", new Object[] { queue_id });
        }

        #endregion

    }
}